package com.panshi.controller;

import com.panshi.UserLoginPort;

import com.panshi.api.*;
import com.panshi.api.util.CommonResult;
import com.panshi.api.vo.UserLoginVo;
import com.panshi.bean.*;
import com.panshi.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class UserController {


    @Autowired
    UserLoginPort userLoginPort;

    

    @Autowired
    private CommonUtil commonUtil;

    @Autowired
    private UserPoth userPoth;

    @Autowired
    private AnswerPoth answerPoth;//回答

    @Autowired
    private ArticlrPoth articlrPoth;

    @Autowired
    private CollectionPoth collectionPoth;

    @Autowired
    private DynamicPoth dynamicPoth;

    @Autowired
    private QuestionPoth questionPoth;





    @GetMapping("/user/main")
    public String userMain(HttpServletRequest request, Model model){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));

        model.addAttribute("user",userQuery);
        return "user/user_main";
    }

    @GetMapping("/user/solo")
    public String userSolo(HttpServletRequest request, Model model){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));
            CountVo queryallcount = userPoth.queryallcount(Integer.valueOf(auto_login));


            model.addAttribute("count",queryallcount);

            model.addAttribute("user",userQuery);
        }

        return "user/user_right_particulars";
    }

    @GetMapping("/user/messages")
    public String userMessages(HttpServletRequest request, Model model){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));
            model.addAttribute("user",userQuery);
        }
        return "user/user_messages";
    }

    @PostMapping("/user/login")
    @ResponseBody
    public CommonResult userLogin(UserLoginVo userLoginVo, String code, HttpSession httpSession, HttpServletResponse response){
        

        log.info(userLoginVo.toString());

        String kaptcha = httpSession.getAttribute("kaptcha").toString();

        log.info("获取图形验证码："+kaptcha+"     输入的验证码："+code);

        CommonResult commonResult =new CommonResult();
        
        if(code!=null && kaptcha.equalsIgnoreCase(code)){
            log.info("验证码正确");
            commonResult=userLoginPort.userlogin(userLoginVo);

            log.info("返回的数据="+commonResult.toString());


            Cookie cookie = new Cookie("1b12ebc9bf976801fd564c64867366c5", commonResult.getData()+"");
            cookie.setPath("/");
            cookie.setMaxAge(60*1000*60*24);//24小时天有效期
            response.addCookie(cookie);

            return commonResult;
        }

        commonResult.setMsg("图形验证码不正确");
        commonResult.setCode("201");
        return commonResult;
    }

    @PostMapping("/user/code")
    @ResponseBody
    public Map<String,Object> auth(String iphone_email, String code, HttpSession httpSession) throws Exception {
        log.info("发送验证码ing...");
        Map<String,Object> map = new HashMap<>();
        String kaptcha = httpSession.getAttribute("kaptcha").toString();
        if(code!=null && kaptcha.equalsIgnoreCase(code)){
           log.info("图形验证码正确      调用短信接口");
           return userLoginPort.getemail(iphone_email);
        }
        map.put("msg","图形验证码不正确");
        map.put("code","201");
        return map;
    }

    @PostMapping("/user/register")
    @ResponseBody
    public CommonResult register(UserLoginVo userLoginVo, String code, HttpSession httpSession,HttpServletResponse response){
        log.info("用户注册ing...");

        String kaptcha = httpSession.getAttribute("kaptcha").toString();

        log.info("获取图形验证码："+kaptcha+"     你输入的验证码："+code);

        if(code!=null && kaptcha.equalsIgnoreCase(code)){
            log.info("验证码正确");
            CommonResult commonResult=userLoginPort.register(userLoginVo);

            log.info("返回的数据="+commonResult.toString());

            Cookie cookie = new Cookie("1b12ebc9bf976801fd564c64867366c5", commonResult.getToken()+"");
            cookie.setPath("/");
            cookie.setMaxAge(60*1000*60*24);//24小时天有效期
            response.addCookie(cookie);

            return commonResult;
        }


        return null;
    }

    @PostMapping("/user/findpwd")
    @ResponseBody
    public CommonResult updatePass(UserLoginVo userLoginVo, String code, HttpSession httpSession, HttpServletResponse response){
        log.info("用户注册ing...");

        String kaptcha = httpSession.getAttribute("kaptcha").toString();

        log.info("获取图形验证码："+kaptcha+"     你输入的验证码："+code);

        if(code!=null && kaptcha.equalsIgnoreCase(code)){
            log.info("验证码正确");
            CommonResult commonResult=userLoginPort.forgetpwd(userLoginVo);

            log.info("返回的数据="+commonResult.toString());

            Cookie cookie = new Cookie("1b12ebc9bf976801fd564c64867366c5", commonResult.getData()+"");
            cookie.setPath("/");
            cookie.setMaxAge(60*1000*60*24);//24小时天有效期
            response.addCookie(cookie);




            return commonResult;
        }
        return null;
    }

    /**
     * 修改用户信息
     * @param userVO
     * @param request
     * @return
     */
    @PostMapping("/user/update")
    @ResponseBody
    public int userUpdate(UserVO userVO,HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        userVO.setUid(Integer.valueOf(auto_login));
        int s=userPoth.updateUser(userVO);

        return s;
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    @PostMapping("/user/updatePass")
    @ResponseBody
    public String updatePass(HttpServletRequest request,UserVO userVO,HttpServletResponse response){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        userVO.setUid(Integer.valueOf(auto_login));

        String str=userPoth.judge(userVO);
        if("ok".equals(str)){
            Cookie cookie = new Cookie("1b12ebc9bf976801fd564c64867366c5", "");//删除前必须要new 一个valu为空；
            cookie.setPath("/");//路径要相同
            cookie.setMaxAge(0);//生命周期设置为0
            response.addCookie(cookie);
        }



        return str;
    }


    /**
     * 个人动态页面
     * @param pageSizeVO
     * @param request
     */
    @PostMapping("/user/index")
    @ResponseBody
    public Object userIndex(PageSizeVO pageSizeVO,HttpServletRequest request,ModelAndView modelAndView){
        //String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        pageSizeVO.setPageSize(10);
        List<DynamicVO> dyQuery = dynamicPoth.findDyQuery(pageSizeVO);

        if(dyQuery.size()==0){
            return "";
        }


        modelAndView.addObject("dynamicList",dyQuery);
        modelAndView.addObject("type","dynamic");
        modelAndView.setViewName("user/user_dynamic");
        return modelAndView;
    }

    /**
     * 个人中心提问
     * @param request
     * @param pageSizeVO
     * @return
     */
    @PostMapping("/user/qalist")
    @ResponseBody
    public Object userQalist(HttpServletRequest request, PageSizeVO pageSizeVO, ModelAndView modelAndView){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");

        //pageSizeVO.setUid(Integer.valueOf(auto_login));
        pageSizeVO.setPageSize(10);

        if((pageSizeVO.getUid()+"").equals(auto_login)){
            modelAndView.addObject("flat","0");
        }else{
            modelAndView.addObject("flat","1");
        }
        System.out.println(pageSizeVO.getUid());
        System.out.println(auto_login);
        System.out.println((pageSizeVO.getUid()+"").equals(auto_login));


        List<List<QuestionVO>> queQuery = questionPoth.findQueQuery(pageSizeVO);
        if(queQuery.get(0).size()==0&&queQuery.get(1).size()==0){
            return "";
        }

        modelAndView.addObject("qaList",queQuery);
        modelAndView.addObject("type","qa");
        modelAndView.setViewName("user/user_dynamic");
        return modelAndView;
    }

    /**
     * 个人中心回答
     * @param request
     * @param pageSizeVO
     * @return
     */
    @PostMapping("/user/qaReplylist")
    @ResponseBody
    public Object userQaReplyList(HttpServletRequest request, PageSizeVO pageSizeVO, ModelAndView modelAndView){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        //pageSizeVO.setUid(Integer.valueOf(auto_login));
        pageSizeVO.setPageSize(10);

        if((pageSizeVO.getUid()+"").equals(auto_login)){
            modelAndView.addObject("flat","0");
        }else{
            modelAndView.addObject("flat","1");
        }

        List<List<AnswerVO>> ansQuery = answerPoth.findAnsQuery(pageSizeVO);

        if(ansQuery.get(0).size()==0&&ansQuery.get(1).size()==0){
            return "";
        }


        modelAndView.addObject("answersList",ansQuery);
        modelAndView.addObject("type","qaReply");
        modelAndView.setViewName("user/user_dynamic");
        return modelAndView;
    }

    /**
     * 个人中心文章
     * @param request
     * @param pageSizeVO
     */
    @PostMapping("/user/articlelist")
    @ResponseBody
    public Object userArticleList(HttpServletRequest request,PageSizeVO pageSizeVO,ModelAndView modelAndView){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
       // pageSizeVO.setUid(Integer.valueOf(auto_login));

        if((pageSizeVO.getUid()+"").equals(auto_login)){
            modelAndView.addObject("flat","0");
        }else{
            modelAndView.addObject("flat","1");
        }

        System.out.println(pageSizeVO);
        pageSizeVO.setPageSize(10);

        List<List<ArticlrVO>> artQuery = articlrPoth.findArtQuery(pageSizeVO);

        if(artQuery.get(0).size()==0&&artQuery.get(1).size()==0){
            return "";
        }
        modelAndView.addObject("articleList",artQuery);
        modelAndView.addObject("type","article");

        modelAndView.setViewName("user/user_dynamic");
        return modelAndView;
    }

    /**
     * 个人收藏
     * @param request
     * @param pageSizeVO
     * @param modelAndView
     */
    @PostMapping("/user/collectList")
    @ResponseBody
    public Object collectList(HttpServletRequest request,PageSizeVO pageSizeVO,ModelAndView modelAndView){
        //String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        //pageSizeVO.setUid(Integer.valueOf(auto_login));
        pageSizeVO.setPageSize(10);

        List<CollectionVO> coQuery = collectionPoth.findCoQuery(pageSizeVO);

        if(coQuery.size()==0){
            return "";
        }
        modelAndView.addObject("collectList",coQuery);
        modelAndView.addObject("type","collect");

        modelAndView.setViewName("user/user_dynamic");
        return modelAndView;
    }

    /**
     * 站内消息
     * @param request
     * @param pageSizeVO
     */
    @PostMapping("/user/msg")
    @ResponseBody
    public void userMsg(HttpServletRequest request, PageSizeVO pageSizeVO, ModelAndView modelAndView){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        pageSizeVO.setUid(Integer.valueOf(auto_login));
        pageSizeVO.setPageSize(10);



    }

    @PostMapping("/sc/del")
    @ResponseBody
    public com.panshi.util.CommonResult<String> scDelete(int cid){
        com.panshi.util.CommonResult<String> stringCommonResult = collectionPoth.deleteCo(cid);
        return stringCommonResult;
    }
	
	@PostMapping("/user/delDy")
    @ResponseBody
    public com.panshi.util.CommonResult<String> delDy(int did){
        com.panshi.util.CommonResult<String> stringCommonResult = dynamicPoth.deleteDy(did);
        return stringCommonResult;
    }

    @GetMapping("/user/personage/{uid}")
    public String userPersonage(@PathVariable int uid,HttpServletRequest request,Model model){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        model.addAttribute("auto",auto_login);
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        //------------------------------上面是顶部信息
        //------------------------------下面是看别人主页信息
        UserVO userQuery1 = userPoth.findUserQuery(uid);//查询用户
        model.addAttribute("userSolo",userQuery1);
        model.addAttribute("type",1);


        CountVo queryallcount = userPoth.queryallcount(uid);
        model.addAttribute("count",queryallcount);






        return "user/user_personage";
    }


}
